package Client_logic;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JOptionPane;
import CLIENTS.Client;

/**
 * This class make the message to server
 * @author ameer
 * @author waleed
 * @author bshara
 * @author kamil
 */
public class ConnectionToServer {
	
	static private Client client = null;

	/**
	 * @param client
	 */
	public ConnectionToServer() throws ClientNotInitializesException{
		super();
		if (client == null)
			throw new ClientNotInitializesException("Cient not initialized");			
	}
		
	/**
	 * @return the client
	 */
	public static synchronized final Client getClient(){
		return client;
	}
	
	/**
	 * This method send to server message 
	 * and return the message that the server send back
	 * @param msgToserver
	 * @return message from server
	 */
	private synchronized final Object sendAndGetFromServer(String[] msgToserver){
		client.handleMessageFromClientUI(msgToserver);
		try{
			int count = 0;
			while (!client.isReadytoread()){
				Thread.sleep(300);
				count++;
				if (count == 10){
					JOptionPane.showMessageDialog(null, "SERVER NOT RESPONDS");
					return null;
				}					
			}				
		}
		catch(InterruptedException exception){
			JOptionPane.showMessageDialog(null, "ERROR!!!!!");
			return null;
		}
		return client.getMessagefromserver();
	}
	 

	/**
	 * This method set static client to newclient
	 * @param newclient
	 */
	public static synchronized final void setClient(Client newclient){
		if (client == null)
			client = newclient;		
	}

	/**
	 * This method sends to server: message= {{AddProduct}, {Price}, {Name}, {description}, {CategoryID}} 
	 * @param product
	 * @return server message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object addProduct(Product product) throws ClientNotInitializesException{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		String[] productarr = product.toArrString();
		if (productarr != null){
			String[] addproductMsg = new String[5];
			addproductMsg[0] = "AddProduct";
			addproductMsg[1] = productarr[0];
			addproductMsg[2] = productarr[1];
			addproductMsg[3] = productarr[2];
			addproductMsg[4] = productarr[3];
			return (sendAndGetFromServer(addproductMsg));
		}
		return null;
	}
	
	/**
	 * This method sends to server: message= {{Login}, {username}, {password}} 
	 * @param username
	 * @param password
	 * @return server message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object userCheck (String username, char[] password) throws ClientNotInitializesException{
		String[] msgtoserver = new String[3];
		String passwordstr = "";
		int i, passwordlength;
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
					
		msgtoserver[0] = "Login";
		msgtoserver[1] = username;
			
		passwordlength = password.length;
		try{
			for (i=0; i < passwordlength; i++){
				passwordstr = passwordstr+password[i];
			}
		}
		catch(Exception exception){
			return null;
		}
		msgtoserver[2] = passwordstr;
		return (sendAndGetFromServer(msgtoserver));		
	}
	
	/**
	 * This method sends to server: message= {{LoadProduct}, {CategoryID}} 
	 * @param CategoryID
	 * @return server message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object loadProduct(long CategoryID) throws ClientNotInitializesException{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		String[] loadmsg = new String[2];
		loadmsg[0] = "LoadProduct";
		loadmsg[1] = String.valueOf(CategoryID);
		return (sendAndGetFromServer(loadmsg));				
	}
	
	/**
	 * This method sends to server: message= {{UpdateProduct}, {Price}, {Name}, {description}, {CategoryID}} 
	 * @param product
	 * @return server message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object updateProduct(Product product) throws ClientNotInitializesException{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		String[] productarr = product.toArrString();
		String[] addproductMsg = new String[5];
		if (productarr != null){
			addproductMsg[0] = "UpdateProduct";
			addproductMsg[1] = productarr[0];
			addproductMsg[2] = productarr[1];
			addproductMsg[3] = productarr[2];
			addproductMsg[4] = productarr[3];
			return (sendAndGetFromServer(addproductMsg));				
		}
		return null;		
	}
	
	/**
	 * 	This method sends to server: message= {{RemoveProduct},{CategoryID}} 
	 * @param CategoryID
	 * @return servers message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object removeProduct(Long CategoryID) throws ClientNotInitializesException{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		
		String[] removeP = new String[2];  
		removeP[0] = "RemoveProduct";
		removeP[1] = String.valueOf(CategoryID);
		return (sendAndGetFromServer(removeP));				
	}
	
	/**
	 * This method sends to server: message= {{LoadAllProductsID}} 
	 * @return servers message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object loadAllProductsID() throws ClientNotInitializesException{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		
		String[] productID = new String[1];  
		productID[0] = "LoadAllProductsID";
		return (sendAndGetFromServer(productID));				
	}
	
	/**
	 * This method sends to server: message= {{CreateTemplate},{TempID}, {TempName}
	 * {Clientmarket}, {message} {productID}}
	 * @return servers message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object createTemplate(Template Template) throws ClientNotInitializesException{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		
		String[] campaindetails = new String[6];
		campaindetails[0] = "CreateTemplate";
		campaindetails[1] = String.valueOf(Template.getTemplateID());
		campaindetails[2] = Template.getTemplateName();
		campaindetails[3] = Template.getClientmarket();
		campaindetails[4] = Template.getMsg();	
		campaindetails[5] = String.valueOf(Template.getProduct().getCategoryID());
		return (sendAndGetFromServer(campaindetails));				
	}
	
	/**
	 * This method sends to server: message= {{loadAllClientsMarket}} 
	 * @return servers message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object loadAllClientsMarket() throws ClientNotInitializesException{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		
		String[] clientmarkets = new String[1];  
		clientmarkets[0] = "LoadAllClientsMarket";
		return (sendAndGetFromServer(clientmarkets));				
	}	
	
	/**
	 * This method sends to server: message = {{UpdateCustomer}, {ID}, {Name}, {Age}, {Area}, {Phone}} 
	 * @return server message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object updateCustomer(Customer customer) throws ClientNotInitializesException{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		String[] customerarr = customer.toTheArrString();
		String[] addCustomerMsg = new String[6];
		if (customerarr != null){
			addCustomerMsg[0] = "UpdateCustomer";
			addCustomerMsg[1] = customerarr[0];
			addCustomerMsg[2] = customerarr[1];
			addCustomerMsg[3] = customerarr[2];
			addCustomerMsg[4] = customerarr[3];
			addCustomerMsg[5] = customerarr[4];
			return (sendAndGetFromServer(addCustomerMsg));				
		}
		return null;		
	}
	
	/**
	 * This method sends to server: message= {{AddCustomer}, {ID}, {Name}, {Age}, {Area},{Phone} 
	 * @return server message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object addCustomer(Customer customer) throws ClientNotInitializesException{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		String[] customerarr = customer.toTheArrString();
		if (customerarr != null){
			String[] addCustomerMsg = new String[6];
			addCustomerMsg[0] = "AddCustomer";
			addCustomerMsg[1] = customerarr[0];
			addCustomerMsg[2] = customerarr[1];
			addCustomerMsg[3] = customerarr[2];
			addCustomerMsg[4] = customerarr[3];
			addCustomerMsg[5] = customerarr[4];
			
			return (sendAndGetFromServer(addCustomerMsg));
		}
		return null;
	}
	
	/**
	 * This method sends to server: message= {{LoadAllAreas}} 
	 * @return servers message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object loadAllAreas() throws ClientNotInitializesException{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		
		String[] clientmarkets = new String[1];  
		clientmarkets[0] = "LoadAllAreas";
		return (sendAndGetFromServer(clientmarkets));				
	}	
	
	/**
	 * This method sends to server: message= {{LoadCustomer}, {clientID}} 
	 * @param clientID
	 * @return server message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object loadCustomer(long clientID) throws ClientNotInitializesException{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		String[] loadmsg = new String[2];
		loadmsg[0] = "LoadCustomer";
		loadmsg[1] = String.valueOf(clientID);
		return (sendAndGetFromServer(loadmsg));				
	}
	
	/**
	 * This method sends to server: message= {{LoadAllCustomers}} 
	 * @return server message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object loadAllCustomers() throws ClientNotInitializesException{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		String[] loadmsg = new String[1];
		loadmsg[0] = "LoadAllCustomers";
		return (sendAndGetFromServer(loadmsg));				
	}
	
	/**
	 * This method sends to server: message= {{CreateTemplate},{TempID}, {TempName}
	 * {Clientmarket}, {message} {productID}}
	 * @return servers message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object updateTemplate(Template Template) throws ClientNotInitializesException{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		
		String[] campaindetails = new String[6];
		campaindetails[0] = "UpdateTemplate";
		campaindetails[1] = String.valueOf(Template.getTemplateID());
		campaindetails[2] = Template.getTemplateName();
		campaindetails[3] = Template.getClientmarket();
		campaindetails[4] = Template.getMsg();	
		campaindetails[5] = String.valueOf(Template.getProduct().getCategoryID());
		return (sendAndGetFromServer(campaindetails));				
	}
	
	/**
	 * This method sends to server: message= {{LoadTemplate}, {TempID}} 
	 * @param TempID
	 * @return server message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object loadTemplate(long TempID) throws ClientNotInitializesException{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		String[] loadmsg = new String[2];
		loadmsg[0] = "LoadTemplate";
		loadmsg[1] = String.valueOf(TempID);
		return (sendAndGetFromServer(loadmsg));				
	}
	
	/**
	 * This method sends to server: message= {{LoadAllTemplates}} 
	 * @return server message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object loadAllTemplates() throws ClientNotInitializesException{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		String[] loadmsg = new String[1];
		loadmsg[0] = "LoadAllTemplates";
		return (sendAndGetFromServer(loadmsg));				
	}
	
	/**
	 * This method sends to server: message= {{LoadAllSection}} 
	 * @return servers message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object loadAllSection() throws ClientNotInitializesException{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		
		String[] clientmarkets = new String[1];  
		clientmarkets[0] = "LoadAllSection";
		return (sendAndGetFromServer(clientmarkets));				
	}	
	
	/**
	 * This method sends to server: message= {{LoadAllSection}, {sectionID}} 
	 * @return servers message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object loadSectionsProduct(int sectionID) throws ClientNotInitializesException{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		
		String[] client = new String[2];  
		client[0] = "LoadSectionsProduct";
		client[1] = String.valueOf(sectionID);
		return (sendAndGetFromServer(client));				
	}	
	
	/**
	 * This method sends to server: message= {{LoadAllSection}, {sectionID}, {customerID}} 
	 * @return servers message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object addAuthorization(int sectionID, long customerID) throws ClientNotInitializesException{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		
		String[] authorization = new String[3];  
		authorization[0] = "AddAuthorization";
		authorization[1] = String.valueOf(sectionID);
		authorization[2] = String.valueOf(customerID);
		return (sendAndGetFromServer(authorization));				
	}	
	
	/**
	 * This method sends to server: message= {{LoadCustomerAuthorization}} 
	 * @return servers message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object loadCustomerAuthorization(long customerID) throws ClientNotInitializesException{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		
		String[] clientauthorization = new String[2];  
		clientauthorization[0] = "LoadCustomerAuthorization";
		clientauthorization[1] = String.valueOf(customerID);
		return (sendAndGetFromServer(clientauthorization));				
	}	
	
	/**
	 * This method sends to server: message= {{UpdateArea},{areaID},{areaName}} 
	 * @return servers message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object updateArea(int areaID, String areaName) throws ClientNotInitializesException{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		
		String[] area = new String[3];  
		area[0] = "UpdateArea";
		area[1] = String.valueOf(areaID);
		area[2] = areaName;
		return (sendAndGetFromServer(area));				
	}	
	
	/**
	 * This method sends to server: message= {{AddArea},{areaID},{areaName}} 
	 * @return servers message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object addArea(int areaID, String areaName) throws ClientNotInitializesException{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		
		String[] area = new String[3];  
		area[0] = "AddArea";
		area[1] = String.valueOf(areaID);
		area[2] = areaName;
		return (sendAndGetFromServer(area));				
	}	
	
	/**
	 * This method sends to server: message= {{LoadAllCategorySections}} 
	 * @return servers message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object loadAllCategorySections() throws ClientNotInitializesException{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		
		String[] clientmarkets = new String[1];  
		clientmarkets[0] = "LoadAllCategorySections";
		return (sendAndGetFromServer(clientmarkets));				
	}	
	
	/**
	 * This method sends to server: message= {{AddCategorySection},{sectionID},{sectionName}} 
	 * @return servers message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object addCategorySection(int sectionID, String sectionName) throws ClientNotInitializesException{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		
		String[] msg = new String[3];  
		msg[0] = "AddCategorySection";
		msg[1] = String.valueOf(sectionID);
		msg[2] = sectionName;
		return (sendAndGetFromServer(msg));				
	}	
	
	/**
	 * This method sends to server: message= {{UpdateCategorySection},{sectionID},{sectionName}} 
	 * @return servers message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object updateCategorySection(int sectionID, String sectionName) throws ClientNotInitializesException{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		
		String[] msg = new String[3];  
		msg[0] = "UpdateCategorySection";
		msg[1] = String.valueOf(sectionID);
		msg[2] = sectionName;
		return (sendAndGetFromServer(msg));				
	}	
	
	/**
	 * This method sends to server: message= {{RemoveCategorySection},{sectionID},{sectionName}} 
	 * @return servers message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object removeCategorySection(int sectionID) throws ClientNotInitializesException{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		
		String[] msg = new String[2];  
		msg[0] = "RemoveCategorySection";
		msg[1] = String.valueOf(sectionID);
		return (sendAndGetFromServer(msg));				
	}	
	
	/**
	 * This method sends to server: message= {{AddCategorytype},{typeID},{typeName}} 
	 * @return servers message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object addType(int TypeID, String TypeName) throws ClientNotInitializesException{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		
		String[] msg = new String[3];  
		msg[0] = "AddCategorytype";
		msg[1] = String.valueOf(TypeID);
		msg[2] = TypeName;
		return (sendAndGetFromServer(msg));				
	}	
	
	/**
	 * This method sends to server: message= {{UpdateType},{typeID},{typeName}} 
	 * @return servers message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object updateType(int typeID, String typeName) throws ClientNotInitializesException{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		
		String[] msg = new String[3];  
		msg[0] = "UpdateType";
		msg[1] = String.valueOf(typeID);
		msg[2] = typeName;
		return (sendAndGetFromServer(msg));				
	}	
	
	/**
	 * This method sends to server: message= {{UpdateType},{typeID},{typeName}} 
	 * @return servers message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object removeType(int typeID) throws ClientNotInitializesException{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		
		String[] msg = new String[2];  
		msg[0] = "RemoveType";
		msg[1] = String.valueOf(typeID);		
		return (sendAndGetFromServer(msg));				
	}
	
	/**
	 * This method sends to server: message= {{LoadAllCategoryType}} 
	 * @return servers message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object loadAllCategoryType() throws ClientNotInitializesException{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		
		String[] clientmarkets = new String[1];  
		clientmarkets[0] = "LoadAllCategoryType";
		return (sendAndGetFromServer(clientmarkets));				
	}	
	
	/**
	 * This method sends to server: message= {{LoadTypeProduct}, {typeID}} 
	 * @return servers message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object loadTypeProduct(int typeID) throws ClientNotInitializesException{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		
		String[] msg = new String[2];  
		msg[0] = "LoadTypeProduct";
		msg[1] = String.valueOf(typeID);
		return (sendAndGetFromServer(msg));				
	}	
	
	/**
	 * This method sends to server: message= {{logOF}} 
	 * @return servers message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object logOf() throws ClientNotInitializesException{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		
		String[] msg = new String[1];  
		msg[0] = "logOF";
		return (sendAndGetFromServer(msg));				
	}	
	
	/**
	 * This method sends to server: message= {{AddProductToType},{typeID},{productID}} 
	 * @return servers message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object addProductToType(int typeID, long productID) throws ClientNotInitializesException{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		
		String[] msg = new String[3];  
		msg[0] = "AddProductToType";
		msg[1] = String.valueOf(typeID);
		msg[2] = String.valueOf(productID);
		return (sendAndGetFromServer(msg));				
	}	
	
	/**
	 * This method sends to server: message= {{RemoveProductFromType},{typeID},{productID}} 
	 * @return servers message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object removeProductFromType(int typeID, long productID) throws ClientNotInitializesException{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		
		String[] msg = new String[3];  
		msg[0] = "RemoveProductFromType";
		msg[1] = String.valueOf(typeID);
		msg[2] = String.valueOf(productID);
		return (sendAndGetFromServer(msg));				
	}	
	
	/**
	 * This method sends to server: message= {{AddTypeToSection},{typeID},{productID}} 
	 * @return servers message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object addTypeToSection(int sectionID, int typeID) throws ClientNotInitializesException{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		
		String[] msg = new String[3];  
		msg[0] = "AddTypeToSection";
		msg[1] = String.valueOf(sectionID);
		msg[2] = String.valueOf(typeID);
		return (sendAndGetFromServer(msg));				
	}	
	
	/**
	 * This method sends to server: message= {{AddProductToType},{typeID},{productID}} 
	 * @return servers message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object removeTypeFromSection(int sectionID, int typeID) throws ClientNotInitializesException{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		
		String[] msg = new String[3];  
		msg[0] = "RemoveTypeFromSection";
		msg[1] = String.valueOf(sectionID);
		msg[2] = String.valueOf(typeID);
		return (sendAndGetFromServer(msg));				
	}	
	
	/**
	 * This method sends to server: message= {{LoadAllSectionType}} 
	 * @return servers message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object loadAllSectionType(int sectionID) throws ClientNotInitializesException{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		
		String[] msg = new String[2];  
		msg[0] = "LoadAllSectionType";
		msg[1] = String.valueOf(sectionID);
		return (sendAndGetFromServer(msg));				
	}	
	
	/**
	 * This method sends to server: message= {{LoadDealSale}} 
	 * @return servers message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object loadDealSale(long DealSaleID) throws ClientNotInitializesException{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		
		String[] msg = new String[2];  
		msg[0] = "LoadDealSale";
		msg[1] = String.valueOf(DealSaleID);
		return (sendAndGetFromServer(msg));				
	}	
	
	/**
	 * This method sends to server: message= {{SaveDealSale},{DealSaleID},
	 * {StartDate},{getEndDate},{Status},{TemplateID}} 
	 * @return servers message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object saveDealSale(DealSale  dealsale) throws ClientNotInitializesException{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		
		String[] msg = new String[6]; 
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
		String datetime;
		msg[0] = "SaveDealSale";
		msg[1] = String.valueOf(dealsale.getDealSaleID());
		datetime = dateFormat.format(dealsale.getStartDate());
		msg[2] = datetime;
		datetime = dateFormat.format(dealsale.getEndDate());
		msg[3] = datetime;
		msg[4] = dealsale.getStatus();
		msg[5] = String.valueOf(dealsale.getTemplate().getTemplateID());
		return (sendAndGetFromServer(msg));				
	}
	
	/**
	 * This method sends to server: message= {{UpdateDealSale},{DealSaleID},
	 * {StartDate},{getEndDate},{Status}} 
	 * @return servers message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object updateDealSale(DealSale  dealsale) throws ClientNotInitializesException
	{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		
		String[] msg = new String[6];  
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
		String datetime;
        msg[0] = "UpdateDealSale";
		msg[1] = String.valueOf(dealsale.getDealSaleID());
		datetime = dateFormat.format(dealsale.getStartDate());
		msg[2] = datetime;
		datetime = dateFormat.format(dealsale.getEndDate());
		msg[3] = datetime;
		msg[4] = dealsale.getStatus();
		return (sendAndGetFromServer(msg));				
	}
	
	/**
	 * This method sends to server: message= {{LoadAllDealSales}} 
	 * @return servers message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object loadAllDealSales() throws ClientNotInitializesException{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		
		String[] msg = new String[1];  
		msg[0] = "LoadAllDealSales";
		return (sendAndGetFromServer(msg));				
	}	
	
	/**
	 * This method sends to server: message= {{LoadAllSellsMem}} 
	 * @return servers message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object loadAllSellsMem() throws ClientNotInitializesException
	{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		
		String[] msg = new String[1];  
		msg[0] = "LoadAllSellsMem";
		return (sendAndGetFromServer(msg));				
	}	
	
	/**
	 * This method sends to server: message= {{LoadAllactiveCampaigns}} 
	 * @return servers message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object loadAllclientAtSector(int sector) throws ClientNotInitializesException
	{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		
		String[] msg = new String[2];  
		msg[0] = "LoadAllclientAtSector";
		msg[1] = String.valueOf(sector);
		return (sendAndGetFromServer(msg));				
	}	
	
	/**
	 * This method sends to server: message= {{LoadAllactiveCampaigns}} 
	 * @return servers message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object updateSellsMenSchedule(String schedule,long activeID) throws ClientNotInitializesException
	{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		
		String[] msg = new String[3];  
		msg[0] = "UpdateSellsMenSchedule";
		msg[1] = schedule;
		msg[2] = String.valueOf(activeID);
		return (sendAndGetFromServer(msg));				
	}	
	
	/**
	 * This method sends to server: message= {{LoadAllactiveCampaigns}} 
	 * @return servers message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object loadAllClientForSellsmen(String username) throws ClientNotInitializesException
	{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		
		String[] msg = new String[2];  
		msg[0] = "loadAllClientForSellsmen";
		msg[1] = username;		
		return (sendAndGetFromServer(msg));				
	}	
	
	/**
	 * This method sends to server: message= {{LoadAllactiveCampaigns}} 
	 * @return servers message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object loadDealSaleForClient(long cID) throws ClientNotInitializesException
	{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		
		String[] msg = new String[2];  
		msg[0] = "loadDealSaleForClient";
		msg[1] = String.valueOf(cID);		
		return (sendAndGetFromServer(msg));				
	}	
	
	/**
	 * This method sends to server: message= {{delayCampaign}  {cID}, {activeID},{response}, {date}} 
	 * @return servers message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object delayTemplate(long cID, long activeID, String response) throws ClientNotInitializesException
	{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		
		String[] msg = new String[5];  
		msg[0] = "delayCampaign";
		msg[1] = String.valueOf(cID);	
		msg[2] = String.valueOf(activeID);	
		msg[3] = response;	
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
        Date date = new Date();
        String datetime = dateFormat.format(date);
        msg[4] = datetime;	
		return (sendAndGetFromServer(msg));				
	}	
	
	/**
	 * This method sends to server: message= {{purchasesCampaign} , {cID}, {activeID}, {date}} 
	 * @return servers message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object purchasesCampaign(long cID, long activeID) throws ClientNotInitializesException
	{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		
		String[] msg = new String[4];  
		msg[0] = "purchasesCampaign";
		msg[1] = String.valueOf(cID);	
		msg[2] = String.valueOf(activeID);	
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
        Date date = new Date();
        String datetime = dateFormat.format(date);
        msg[3] = datetime;	
		return (sendAndGetFromServer(msg));				
	}	
	
	/**
	 * This method sends to server: message= {{purchasesCampaign} , {cID}, {activeID}, {date}} 
	 * @return servers message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object getResponseReport(long activeID) throws ClientNotInitializesException
	{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		
		String[] msg = new String[2];  
		msg[0] = "getResponseReport";		
		msg[1] = String.valueOf(activeID);	
		return (sendAndGetFromServer(msg));				
	}	
	
	/**
	 * This method sends to server: message= {{analyzesSystem}} 
	 * @return servers message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object analyzesSystem() throws ClientNotInitializesException
	{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		
		String[] msg = new String[1];  
		msg[0] = "analyzesSystem";		
		return (sendAndGetFromServer(msg));				
	}
	
	/**
	 * This method sends to server: message= {{customerReport}} 
	 * @return servers message
	 * @throws ClientNotInitializesException
	 */
	public synchronized Object customerReport() throws ClientNotInitializesException
	{
		if (client == null)
			throw new ClientNotInitializesException("Client not initialized");
		
		String[] msg = new String[1];  
		msg[0] = "customerReport";		
		return (sendAndGetFromServer(msg));				
	}	
	
	
	
}
